<?php

namespace App\Http\Controllers\Api;

use App\Exports\TalentIndexExport;
use App\Exports\TalentSpecialExport;
use App\Http\Requests\Api\TalentRequest;
use App\Imports\TalentImport;
use App\Models\Batch;
use App\Models\Talent;
use App\Transformers\TalentTransfromer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

class TalentController extends Controller
{
    public function index(Request $request, Talent $talent)
    {
        $talent = $this->_getIndexList($request, $talent);
        $talents = $talent->orderBy('created_at', 'desc')->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    private function _getIndexList($request, $talent)
    {
        $talent = $talent->newQuery();
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }
        if ($request->input('interview_status') || $request->input('interview_status') === 0 || $request->input('interview_status') === '0') {
            $talent->where('interview_status', $request->input('interview_status'));
        }
        if ($request->input('education')) {
            $talent->where('education', $request->input('education'));
        }
        if ($request->input('recommend')) {
            $talent->where('recommend', $request->input('recommend'));
        }

        return $talent;
    }

    public function export_index(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $excel = '人才库-'.$datetime.'.xlsx';
            return Excel::download(new TalentIndexExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function special(Request $request, Talent $talent)
    {
        $talent = $talent->newQuery();
        $talent->where('interview_status', (int) $request->input('interview_status'));
        if ($request->has('tan_xin_status')) {
            $talent->where('tan_xin_status', (int) $request->input('tan_xin_status'));
        }
        if ($request->has('tijian')) {
            $talent->where('tijian', (int) $request->input('tijian'));
        }
        if ($request->has('send_offer')) {
            $talent->where('send_offer', (int) $request->input('send_offer'));
        }
        if ($request->input('name')) {
            $talent->where('name', $request->input('name'));
        }
        if ($request->input('sex') || $request->input('sex') === 0 || $request->input('sex') === '0') {
            $talent->where('sex', $request->input('sex'));
        }
        if ($request->input('interview_status') || $request->input('interview_status') === 0 || $request->input('interview_status') === '0') {
            $talent->where('interview_status', $request->input('interview_status'));
        }
        if ($request->input('education')) {
            $talent->where('education', $request->input('education'));
        }
        $talents = $talent->paginate(20);

        return $this->response->paginator($talents, new TalentTransfromer());
    }

    public function export_special(Request $request)
    {
        if ($request->has('do') && $request->input('do') === 'export') {
            $datetime = date('Y-m-d-H-i-s');
            $name = '';
            switch ($request->input('interview_status')) {
                case 3:
                    $name = '一面未通过';
                    break;
                case 4:
                    $name = '一面缺席';
                    break;
                case 7:
                    $name = '二面未通过';
                    break;
                case 8:
                    $name = '二面缺席';
                    break;
                case 11:
                    if ($request->input('tan_xin_status') === 2) {
                        $name = '薪资未谈妥';
                    } elseif ($request->input('tijian') === 2) {
                        $name = '体检未通过';
                    } elseif ($request->input('send_offer') === 2) {
                        $name = 'OFFER未接受';
                    }
                    break;
            }
            $excel = $name.'-'.$datetime.'.xlsx';
            return Excel::download(new TalentSpecialExport, $excel);
        } else {
            return response()->json([
                'url' => $request->fullUrl().'&do=export'
            ]);
        }
    }

    public function store(TalentRequest $request)
    {
        $talent = Talent::create([
            'name' => $request->name,
            'recommend' => $request->recommend ?? '',
            'first_recommend' => $request->first_recommend ?? '',
            'sex' => $request->sex,
            'birthday' => $request->birthday ?? '',
            'education' => $request->education ?? '',
            'university' => $request->university ?? '',
            'profession' => $request->profession ?? '',
        ]);

        return $this->response->item($talent, new TalentTransfromer())
            ->setStatusCode(201);
    }

    public function update(TalentRequest $request, Talent $talent)
    {
        $talent->update([
            'name' => $request->name,
            'recommend' => $request->recommend,
            'first_recommend' => $request->first_recommend,
            'sex' => $request->sex,
            'birthday' => $request->birthday,
            'education' => $request->education,
            'university' => $request->university,
            'profession' => $request->profession,
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function delete(Talent $talent)
    {
        $talent->delete();

        return $this->response->noContent();
    }

    public function upload(Request $request)
    {
        $file = $request->file('file');
        $project_id = $request->input('project_id');
        $ext = $file->getClientOriginalExtension();
        if (!in_array($ext, ['xls', 'xlsx'])) {
            return response()->json([
                'message' => '文件类型错误',
                'status' => 403
            ]);
        }

        Excel::import(new TalentImport($project_id), $file);

        return $this->response->created();
    }

    public function upload_resume(Request $request)
    {
        $talent = Talent::find($request->input('id'));
        $path = $request->file('file')->store('public/resumes');
        $url = str_replace('public', 'storage', $path);
        $talent->update([
            'resume' => $url,
        ]);
        
        return $this->response->created();
    }

    public function send_waiting(Request $request, Talent $talent)
    {
        $timestamp = (int) $request->input('time');
//        $date = date('Ymd', $timestamp);
//        $time = date('H', $timestamp);
//        if ((int) $time <= 12) {
//            $batch = $date.'AM'.$time;
//        } else {
//            $batch = $date.'PM'.((int) $time - 12);
//        }
        $date = date('Y-m-d', $timestamp);
        $datetime = date('YmdHi', $timestamp);
        $datetime2 = date('Y-m-d H:i', $timestamp);
        $group = $request->input('group');
        $batch_name = $datetime.$group;
        $batch = Batch::where('name', $batch_name)->where('type', 1)->first();
        $batch_id = null;
        if ($batch) {
            $batch_id = $batch->id;
        } else {
            $b = Batch::create([
                'name' => $batch_name,
                'time' => $timestamp,
                'batch_date' => $date,
                'batch_datetime' => $datetime2,
                'group' => $group,
                'type' => 1,
            ]);
            $batch_id = $b->id;
        }
        $talent->update([
            'interview_status' => 1,
            'first_batch_id' => $batch_id,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function send_waiting2(Talent $talent)
    {
        $talent->update([
            'interview_status' => 1,
        ]);

        return $this->response->item($talent, new TalentTransfromer());
    }

    public function send_first(Request $request, Talent $talent)
    {
//        $timestamp = (int) $request->input('time');
//        $date = date('Y-m-d', $timestamp);
//        $datetime = date('YmdHi', $timestamp);
//        $datetime2 = date('Y-m-d H:i', $timestamp);
        $iDate = $request->input('time');
        $timestamp = (int) strtotime($iDate);
        $datetime = date('Y-m-d H:i', strtotime($iDate));

        $group = $request->input('group');
        $batch_name = '1-'.$iDate.$group;
        $batch = Batch::where('name', $batch_name)->where('type', 1)->first();
        $batch_id = null;
        if ($batch) {
            $batch_id = $batch->id;
        } else {
            $b = Batch::create([
                'name' => $batch_name,
                'time' => $timestamp,
                'batch_date' => $iDate,
                'batch_datetime' => $datetime,
                'group' => $group,
                'type' => 1,
            ]);
            $batch_id = $b->id;
        }
        $talent->update([
            'interview_status' => 2,
            'first_batch_id' => $batch_id,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function send_second(Request $request, Talent $talent)
    {
//        $timestamp = (int) $request->input('time');
//        $date = date('Y-m-d', $timestamp);
//        $datetime = date('YmdHi', $timestamp);
//        $datetime2 = date('Y-m-d H:i', $timestamp);

        $iDate = $request->input('time');
        $timestamp = (int) strtotime($iDate);
        $datetime = date('Y-m-d H:i', strtotime($iDate));

        $group = $request->input('group');
        $batch_name = '2-'.$iDate.$group;
        $batch = Batch::where('name', $batch_name)->where('type', 2)->first();
        $batch_id = null;
        if ($batch) {
            $batch_id = $batch->id;
        } else {
            $b = Batch::create([
                'name' => $batch_name,
                'time' => $timestamp,
                'batch_date' => $iDate,
                'batch_datetime' => $datetime,
                'group' => $group,
                'type' => 2,
            ]);
            $batch_id = $b->id;
        }
        $talent->update([
            'interview_status' => 6,
            'second_batch_id' => $batch_id,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function send_hr(Request $request, Talent $talent)
    {
//        $timestamp = (int) $request->input('time');
//        $date = date('Y-m-d', $timestamp);
//        $datetime = date('YmdHi', $timestamp);
//        $datetime2 = date('Y-m-d H:i', $timestamp);

        $iDate = $request->input('time');
        $timestamp = (int) strtotime($iDate);
        $datetime = date('Y-m-d H:i', strtotime($iDate));

        $group = $request->input('group');
        $batch_name = '3-'.$iDate.$group;
        $batch = Batch::where('name', $batch_name)->where('type', 3)->first();
        $batch_id = null;
        if ($batch) {
            $batch_id = $batch->id;
        } else {
            $b = Batch::create([
                'name' => $batch_name,
                'time' => $timestamp,
                'batch_date' => $iDate,
                'batch_datetime' => $datetime,
                'group' => $group,
                'type' => 3,
            ]);
            $batch_id = $b->id;
        }
        $talent->update([
            'interview_status' => 10,
            'hr_batch_id' => $batch_id,
        ]);
        return $this->response->item($talent, new TalentTransfromer());
    }

    public function mul_waiting(Request $request)
    {
        $talents = Talent::whereIn('id', $request->input('ids'))->update([
            'interview_status' => 1,
        ]);

        return $this->response->array($talents, new TalentTransfromer());
    }

    public function download_template()
    {
        $url = config('app.url').'/templates/template.xlsx';
        return $url;
    }
}
